﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Windows.Forms;

namespace TreeListView
{
    public partial class TreeListViewControl : UserControl, TreeListView.IColumnsProvider
    {
        /// <summary>
        /// Add A Comment Here O.o
        /// </summary>
        [System.Diagnostics.DebuggerNonUserCode()]
        public TreeListViewControl()
        {
            InitializeComponent();
            DoubleBuffered = true;
            treeView.ColumnProvider = this;
        }

        /// <summary>
        /// Add A Comment Here O.o
        /// </summary>
        public const char ColumnSeperator = '^';

        /// <summary>
        /// Add A Comment Here O.o
        /// </summary>
        [MergableProperty(false)]
        [Editor("System.Windows.Forms.Design.ColumnHeaderCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Localizable(true)]
        [System.Diagnostics.DebuggerNonUserCode()]
        public ListView.ColumnHeaderCollection Columns
        {
            [System.Diagnostics.DebuggerNonUserCode()]
            get
            {
                return listView.Columns;
            }
        }

        /// <summary>
        /// Add A Comment Here O.o
        /// </summary>
        public TreeView TreeView { get { return treeView; } }

        public int TotalColWidth
        {
            [System.Diagnostics.DebuggerNonUserCode()]
            get
            {
                int width = 0;
                foreach (var col in Columns.Cast<ColumnHeader>()) { width += col.Width; }
                return width;
            }
        }

        private bool m_scrollbarIsHidden;
        private Panel m_scrollbarBlanket;

        private Panel ScrollbarBlanket
        {
            get
            {
                if (m_scrollbarBlanket == null)
                {
                    m_scrollbarBlanket = new Panel();
                    m_scrollbarBlanket.BackColor = SystemColors.ControlLight;
                    int scrollbarHeight = 20;
                    m_scrollbarBlanket.Parent = this;
                    m_scrollbarBlanket.Location = new Point(2, this.Height - scrollbarHeight);
                    m_scrollbarBlanket.Size = new Size(Width - 4, scrollbarHeight);
                    m_scrollbarBlanket.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
                }
                return m_scrollbarBlanket;
            }
        }

        public string CreateMultiColumnNodeText(params string[] texts)
        {
            return string.Concat(texts.Select(t => t.Replace(ColumnSeperator.ToString(), "") + ColumnSeperator.ToString()).ToArray());//.PadRight(400);
        }

        private void listView_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
        {
            treeView.Refresh();
        }

        private void listView_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            if (TotalColWidth > ClientSize.Width)
            {
                e.Cancel = true;
                e.NewWidth = ClientSize.Width - (TotalColWidth - Columns[e.ColumnIndex].Width);
            }
            treeView.Refresh();
        }

        /// <summary>
        /// Add A Comment Here O.o
        /// </summary>
        public List<TreeNode> TreeNodeListRecursive
        {
            get { return treeView.TreeNodeListRecursive; }
        }

        /// <summary>
        /// Add A Comment Here O.o
        /// </summary>
        public List<TreeNode> ExposedTreeNodes
        {
            get { return treeView.ExposedNodes; }
        }

        private void treeView_HideOrShowScrollBar(object sender, MultiColumnTreeView.HideScrollBarEventArgs e)
        {
            if (e.Hide && !m_scrollbarIsHidden)
            {
                ScrollbarBlanket.BringToFront();
                ScrollbarBlanket.Visible = true;
                ScrollbarBlanket.Show();
                m_scrollbarIsHidden = true;
            }
            else if (!e.Hide && m_scrollbarIsHidden)
            {
                ScrollbarBlanket.Visible = false;
                ScrollbarBlanket.Hide();
                m_scrollbarIsHidden = false;
            }
        }

        private void TreeListViewControl_EnabledChanged(object sender, EventArgs e)
        {
            treeView.Enabled = this.Enabled;
            listView.Enabled = this.Enabled;
            if (Enabled)
                listView.HeaderStyle = ColumnHeaderStyle.Clickable;
            else
                listView.HeaderStyle = ColumnHeaderStyle.Nonclickable;
        }

        /// <summary>
        ///
        /// </summary>
        /// <remarks>Aaron Added This and MultiColumnTreeView_NodeMouseClick</remarks>
        //public event TreeViewEventHandler AfterSelect;

        //public TreeViewEventArgs AfterSelect
        //{
        //    set
        //    {
        //        this.treeView.AfterSelect += new TreeViewEventHandler()
        //    }
        //}

        public Action<TreeNode> AfterClick
        {
            get { return this.treeView.AfterClick; }
            set { this.treeView.AfterClick = value; }
        }

        //private void MultiColumnTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        //{
        //    //this.treeView.AfterSelect += AfterSelect;

        //    //this.treeView.MultiColumnTreeView_AfterSelect

        //    this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.AfterSelect);

        //    //if (AfterSelect != null) AfterSelect.Invoke(this, this.treeView.MultiColumnTreeView_AfterSelect());
        //}
    }
}